//
//  TimerLabel.m
//  BCNADemo
//
//  Created by 马东朝 on 2018/5/14.
//  Copyright © 2018年 马东朝. All rights reserved.
//

#import "TimerLabel.h"
@interface TimerLabel(){
    CGRect mainFrame;
    NSTimer *timer;
}
@property (nonatomic, strong) UILabel *timLabel;
@end
static int timeNum = 0;

@implementation TimerLabel

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        mainFrame = frame;
        [self addSubview:self.timLabel];
        timer= [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
    }
    return self;
}
- (UILabel *)timLabel{
    if (!_timLabel) {
        _timLabel = [[UILabel alloc] initWithFrame:mainFrame];
        _timLabel.textAlignment = NSTextAlignmentCenter;
        _timLabel.font = [UIFont systemFontOfSize:kHeight(12)];
        _timLabel.text = @"00:00:00";
    }
    return _timLabel;
}
- (void)start{
     [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)updateTime{
    timeNum ++;
    _timLabel.text =  [NSString stringWithFormat:@"%02d:%02d:%02d",timeNum %(3600*12)/3600,timeNum%3600/60,timeNum%60];
}
- (void)stop{
    [timer invalidate];
    timer = nil;
}
@end
